#include "mainwindow.h"
#include "./ui_mainwindow.h"

#include <QDesignerComponents>
#include <QDesignerFormWindowManagerInterface>
#include <QFile>
#include <QHBoxLayout>
#include <private/widgetdatabase_p.h>
#include <QDesignerFormEditorPluginInterface>
#include <QDesignerIntegration>
#include <QDesignerMetaDataBaseInterface>
#include <QDesignerWidgetBoxInterface>
#include <QQueue>
#include <QRandomGenerator>
#include <private/pluginmanager_p.h>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

int MainWindow::init()
{
    QDesignerComponents::initializeResources();


    m_formEditor = QDesignerComponents::createFormEditor(nullptr);


    QDesignerComponents::initializePlugins(m_formEditor);

    auto widgetBox = QDesignerComponents::createWidgetBox(m_formEditor, nullptr);

    widgetBox->show();
    m_formEditor->setTopLevel(widgetBox);
    m_formEditor->setWidgetBox(widgetBox);

    auto integration = new QDesignerIntegration(m_formEditor, this);
    m_formEditor->setIntegration(integration);


    QDesignerFormWindowManagerInterface *formWindowManager = m_formEditor->formWindowManager();


    //创建用于拖放控件的界面
    auto dialog = new QWidget;

    auto *formWindow = formWindowManager->createFormWindow(dialog);

    m_widget = formWindow;

    QDesignerFormWindowInterface::Feature fe{QDesignerFormWindowInterface::EditFeature};
    formWindow->setFeatures(fe);
    formWindow->setAcceptDrops(true);


    QHBoxLayout *hbl = new QHBoxLayout;
    hbl->setSpacing(0);
    hbl->setContentsMargins(0, 0, 0, 0);

    hbl->addWidget(formWindow);
    dialog->setLayout(hbl);

    QString contents = qdesigner_internal::WidgetDataBase::formTemplate(m_formEditor, "QFrame", "frame");

    if (!formWindow->setContents(contents))
    {
        formWindowManager->removeFormWindow(formWindow);
        m_formEditor->metaDataBase()->remove(formWindow);

        return -1;
    }

    dialog->show();

    dialog->setWindowTitle("测试");

    dialog->resize(400, 300);

    formWindowManager->setActiveFormWindow(formWindow);


    return 0;
}

void MainWindow::on_pushButton_clicked()
{
    QQueue<QWidget*> ws;

    ws.enqueue(m_widget);

    while(!ws.isEmpty())
    {
        auto w = ws.dequeue();

        auto child = w->children();

        for(auto& ch : child)
        {
            if(ch->isWidgetType())
                ws.enqueue((QWidget*)ch);

            auto button = qobject_cast<QPushButton*>(ch);

            if(button)
                button->setText(QString::number(QRandomGenerator::system()->generate()));
        }

    }
    auto child = m_widget->children();

    for(auto& ch : child)
    {
        auto button = qobject_cast<QPushButton*>(ch);

        if(button)
            button->setText(QString::number(QRandomGenerator::system()->generate()));
    }
}

